#coding=utf-8
__author__ = 'liangdong'
from numpy import *

##文本内容转化为矩阵
def file2matrix(filename):
    fr = open(filename)
    numberOfLines = len(fr.readlines())         #得到文件行数
    returnMat = zeros((numberOfLines,3))        #初始化返回矩阵
    classLabelVector = []                       #初始化返回标签
    #解析文件内容到矩阵
    fr = open(filename)
    index = 0
    for line in fr.readlines():
        line = line.strip()
        listFromLine = line.split('\t')
        returnMat[index,:] = listFromLine[0:3]
        classLabelVector.append(int(listFromLine[-1]))
        index += 1
    return returnMat,classLabelVector

##归一化
def autoNorm(dataSet):
    minVals = dataSet.min(0)    #每列的最小值放在变量minVals
    maxVals = dataSet.max(0)    #每列的最大值放在变量maxVals
    ranges = maxVals - minVals
    normDataSet = zeros(shape(dataSet))
    m = dataSet.shape[0]
    normDataSet = dataSet - tile(minVals, (m,1))
    normDataSet = normDataSet/tile(ranges, (m,1))   #特征值相除
    return normDataSet, ranges, minVals